package v1

import (
	"errors"
	"net/http"
	"strconv"
	"zhejianglab/gin-apiserver/model"
	"zhejianglab/gin-apiserver/model/request"

	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
)

// AddNewDownload create new Download
// @Produce  json
// @Tags Download
// @Param AddNewDownload body request.DownloadInfo true "request.DownloadInfo"
// @Success 201 {object} gin.H "{"code":201,"msg":"AddNewDownload successfully"}"
// @Failure 409 {object} gin.H "{"code":500,"msg":"AddNewDownload: Download name is exist"}"
// @Failure 500 {object} gin.H "{"code":500,"msg":"AddNewDownload: Download name is invalid"}"
// @Router /api/v1/download/new [post]
func AddNewDownload(c *gin.Context) {
	var err error

	var Download request.DownloadInfo
	_ = c.ShouldBindJSON(&Download)
	name := Download.Name
	cameraID := Download.CameraID
	beginTs := Download.BeginTs
	endTs := Download.EndTs
	vmsID := Download.VmsID
	process := Download.Process

	valid := validation.Validation{}

	// 校验视频名、fileKey的有效性
	valid.Required(name, "name").Message("视频名称不能为空")
	if valid.HasErrors() {
		err = errors.New("AddNewDownload: Download name is invalid")
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": err.Error()})
		return
	}

	if model.ExistDownloadByName(name) {
		err = errors.New("AddNewDownload: Download name is exist")
		c.JSON(http.StatusConflict, gin.H{"status": http.StatusConflict, "message": err.Error()})
		return
	}

	model.AddNewDownload(name, beginTs, endTs, cameraID, vmsID, process)
	c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated, "message": "AddNewDownload successfully!", "data": name})
}

// GetDownload get Download info from db
// @Produce  json
// @Tags Download
// @Param id path int true "ID"
// @Success 200 {object} gin.H "{"code":200,"msg":"GetDownload successfully"}"
// @Failure 500 {object} gin.H "{"code":500,"msg":"GetDownload failed!"}"
// @Router /api/v1/download/{id} [get]
func GetDownload(c *gin.Context) {
	id := c.Param("id")
	data, err := model.GetDownload(id)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "GetDownload successfully!", "data": data})
}

// UpdateDownload create new Downloadtithm
// @Produce  json
// @Tags Download
// @Param DownloadInfo body request.DownloadInfo true "request.DownloadInfo"
// @Success 201 {object} gin.H "{"code":201,"msg":"UpdateDownload successfully"}"
// @Failure 409 {object} gin.H "{"code":500,"msg":"UpdateDownload: Download name is exist"}"
// @Failure 500 {object} gin.H "{"code":500,"msg":"UpdateDownload: Download name is invalid"}"
// @Router /api/v1/download/{id} [put]
func UpdateDownload(c *gin.Context) {
	var err error
	data := make(map[string]interface{})
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": err.Error()})
		return
	}
	if !model.ExistDownloadByID(id) {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "Download id is not exists failed!"})
		return
	}

	var Download request.DownloadInfo

	err = c.ShouldBindJSON(&Download)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "UpdateDownload failed!" + err.Error()})
		return
	}

	if Download.Name != "" {
		if model.ExistDownloadByName(Download.Name) {
			err = errors.New("UpdateDownload: Download name is exist")
			c.JSON(http.StatusConflict, gin.H{"status": http.StatusConflict, "message": err.Error(), "Download name": Download.Name})
			return
		}
		data["name"] = Download.Name
	}

	if Download.CameraID != 0 {
		data["cameraID"] = Download.CameraID
	}
	if Download.BeginTs != "" {
		data["begints"] = Download.BeginTs
	}

	if Download.EndTs != "" {
		data["endts"] = Download.EndTs
	}

	if Download.VmsID != 0 {
		data["vmsID"] = Download.VmsID
	}

	if Download.Process != 0 {
		data["process"] = Download.Process
	}

	model.UpdateDownload(id, data)
	c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "UpdateDownload successfully!", "data": data})
	return

}

// DeleteDownload delete case info from db
// @Produce  json
// @Tags Download
// @Param id path int true "ID"
// @Success 200 {object} gin.H "{"code":200,"msg":"DeleteDownload successfully"}"
// @Failure 500 {object} gin.H "{"code":500,"msg":"DeleteDownload failed!"}"
// @Failure 500 {object} gin.H "{"code":500,"msg":"id is invalid!"}"
// @Router /api/v1/Download/{id} [delete]
func DeleteDownload(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": err.Error()})
		return
	}

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("ID必须大于0")

	if !valid.HasErrors() {
		if model.ExistDownloadByID(id) {
			model.DeleteDownload(id)
			c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "DeleteDownload successfully!"})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "DeleteDownload failed!"})
			return
		}
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": "id is invalid!"})
		return
	}
}
